<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="城市：">
          <el-select v-model="formInline.city" filterable clearable>
            <el-option v-for="(item, index) in cityList" :key="index" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否认证：">
          <el-select v-model="formInline.hasCertified" clearable>
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态：">
          <shelf-select v-model="formInline.onShelf"></shelf-select>
        </el-form-item>
        <el-form-item label="开放预约：">
          <el-select v-model="formInline.isOpened" clearable>
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否首页：">
          <el-select v-model="formInline.homePageShow" clearable>
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商务运营人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="商务拓展人：">
          <user-select v-model="formInline.businessDeveloper"></user-select>
        </el-form-item>
        <el-form-item label="门诊分类：">
          <el-select v-model="formInline.outpatientClass" clearable>
            <el-option label="AA" value="AA"></el-option>
            <el-option label="SA" value="SA"></el-option>
            <el-option label="A" value="A"></el-option>
            <el-option label="B" value="B"></el-option>
            <el-option label="C" value="C"></el-option>
            <el-option label="D" value="D"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入关键词"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="onSearch">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <auth-button uid="1060502" type="primary" size="small" @click.native="addHospital()">新增医院</auth-button>
        <auth-button uid="1060505" type="primary" icon="el-icon-download" :loading="eloading" size="small" @click.native="downloadExcel()">批量导出</auth-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData" @sort-change="sortChange">
        <el-table-column prop="channelName" label="医院信息" width="400px">
          <template slot-scope="{row}">
            <tab-hospital :user="showFormatter(row)"></tab-hospital>
          </template>
        </el-table-column>
        <el-table-column prop="initAppointmentCnt" label="展示预约" show-overflow-tooltip />
        <el-table-column prop="initScore" label="展示评分" show-overflow-tooltip />
        <el-table-column prop="hasCertified" label="是否认证" show-overflow-tooltip>
          <template slot-scope="{row}">{{ row.hasCertified == 1 ? '是' : '否' }}</template>
        </el-table-column>
        <el-table-column prop="onShelf" label="是否上架" show-overflow-tooltip>
          <template slot-scope="{row}"><shelf-format :value="row.onShelf"></shelf-format></template>
        </el-table-column>
        <el-table-column prop="homePageShow" label="是否首页" show-overflow-tooltip>
          <template slot-scope="{row}">{{ row.homePageShow == 1 ? '是' : '否' }}</template>
        </el-table-column>
        <el-table-column prop="isOpened" label="是否开放预约" show-overflow-tooltip :width="tdWidth">
          <template slot-scope="{row}">{{ row.isOpened == 1 ? '是' : '否' }}</template>
        </el-table-column>
        <el-table-column prop="outpatientClass" label="门诊分类" show-overflow-tooltip />
        <el-table-column prop="principal" label="商务运营人" show-overflow-tooltip :width="tdWidth" />
        <el-table-column prop="businessDeveloper" label="商务拓展人" show-overflow-tooltip :width="tdWidth" />
        <el-table-column prop="orderTime" label="排序时间" sortable="custom" show-overflow-tooltip :width="tdWidth">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" @click="openDialog(row)">{{ row.orderTime }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="onlineTime" label="上线时间" show-overflow-tooltip />
        <el-table-column prop="statementDate" label="出账日期" show-overflow-tooltip />
        <el-table-column label="操作" width="200px" align="center" fixed="right">
          <template slot-scope="{row}">
            <auth-link uid="1060501" type="primary" class="el_link" :underline="false" :id="row.id"
              @click.native="lookStatic(row)">数据分析</auth-link>
            <auth-link uid="1060502" type="primary" class="el_link" :underline="false" :id="row.id"
              @click.native="addHospital(row.id)">编辑</auth-link>
            <auth-link uid="1060503" type="primary" class="el_link" :underline="false" :id="row.id"
              @click.native="removeHospital(row)">删除</auth-link>
            <!-- <el-link type="primary" class="el_link" :underline="false" :id="row.id" @click="lookStatic(row)">数据分析</el-link>
              <el-link type="primary" class="el_link" :underline="false" :id="row.id" @click="addHospital(row.id)">编辑</el-link>
              <el-link type="danger" class="el_link" :underline="false" @click="removeHospital(row)">删除</el-link> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize"
          :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList"
          @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <glsx-dialog :show="showTimeDialog" title="修改排序时间" @close="closeDialog()">
      <div class="">
        <el-form ref="editForm" :model="editForm" :rules="editFormRules" size="small" label-width="100px">
          <el-form-item label="排序时间" prop="orderTime">
            <el-date-picker v-model="editForm.orderTime" type="date" placeholder="选择排序时间"
              value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="">
            <el-button type="primary" :loading="loading" @click="updateTime">保存</el-button>
            <el-button type="default" @click="closeDialog()">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </glsx-dialog>

  </div>
</template>
  
<script>
import { GETCITYOPTIONS } from "@/http/system";
import { showToast, showModal, deepClone } from "@/common/util";
import { exportExcel } from "@/common/common";
import TabHospital from "@/components/hospital";
import { GET_HOSPITAL_SEARCHALL, DELETEHOSPITALINFO, UPDATE_HOSPITAL_ORDERTIME,EXPORT_HOSPITAL_LIST } from "@/http/hospital";
export default {
  components: {
    TabHospital,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      cityList: [],
      tdWidth: 120,
      showTimeDialog: false,
      editForm: {},
      editFormRules: {
        orderTime: [{ required: true, message: '请选择排序时间', trigger: 'change' }]
      },
      eloading:false
    };
  },
  mounted() {
    this.getList();
    this.getCity();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = {};
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = JSON.parse(JSON.stringify(this.formInline));

      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GET_HOSPITAL_SEARCHALL(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    // 新增医院
    addHospital(id) {
      this.$router.push({ name: "allHospitalAdd", query: { id: id } });
    },

    // 格式化医院数据
    showFormatter(row) {
      if (!row) {
        return {};
      }
      row.hospitalName = row.name;
      row.hospitalAddress = row.address;
      return row;
    },

    // 删除医院
    removeHospital(row) {
      let _this = this;
      showModal({
        message: `确定删除${row.name}吗？`,
        showCancelButton: true,
        success: function () {
          DELETEHOSPITALINFO({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 获取所有城市
    getCity() {
      GETCITYOPTIONS().then((res) => {
        this.cityList = res.data || [];
      });
    },

    // 查看数据分析
    lookStatic(row) {
      this.$router.push({
        name: "hospitalAllStatic",
        query: { hospital: row.id, name: row.name },
      });
    },

    // 搜索
    onSearch() {
      this.pageNumber = 1;
      this.getList()
    },

    // 监听表格排序
    sortChange({ column, prop, order }) {
      let o = { orderField: prop };
      this.formInline.orderField = prop;
      if (order == "ascending") {
        this.formInline.orderDirection = "ASC";
      } else {
        this.formInline.orderDirection = "DESC";
      }
      this.getList();
    },

    // 打开修改时间弹框
    openDialog(row) {
      this.showTimeDialog = true;
      this.current = row
    },

    // 关闭弹框
    closeDialog() {
      this.showTimeDialog = false;
      this.current = {}
    },

    // 保存时间
    updateTime() {
      let _this = this;
      this.$refs.editForm.validate(valid => {
        if (!valid) {
          return false;
        }
        let params = deepClone(_this.editForm)
        params.id = this.current.id;
        _this.loading = true
        UPDATE_HOSPITAL_ORDERTIME(params).then(res => {
          _this.loading = false
          if (res.code == 200) {
            showToast({
              message: '修改成功',
              type: 'success'
            })
            _this.closeDialog();
            _this.getList()
          }
        })

      })
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有医院列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }
      showToast({
        message: "请选择筛选条件后再导出",
      });

      //_this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};
      //let list = this.checkList;
      //let arr = [];

      // if (!list || list.length <= 0) {
      //   showToast({
      //     message: "请选择要导出的医院",
      //   });
      //   return false;
      // }

      // list.map((k) => {
      //   arr.push(k.id);
      // });

      //oParams.checkedIds = arr.toString();

      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = EXPORT_HOSPITAL_LIST;
      options.fileName = "所有医院列表.xlsx";
      _this.eloading = true;
      exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },


  },
};
</script>
  
<style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
</style>
  